package edu.nctu.csie.jichang.database.command;

import edu.nctu.csie.jichang.database.model.cell.DBDatabase;
import edu.nctu.csie.jichang.database.model.cell.DBRow;
import edu.nctu.csie.jichang.database.model.cell.DBSchema;
import edu.nctu.csie.jichang.database.model.cell.DBTable;

public class DifferenceCommand implements IDBCommand{

	private DBDatabase element1 = null;
	private DBDatabase element2 = null;

	public DBDatabase execute() {
		for(String tName : element1.getSchemaNames()){
			if(element2.hasSchema(tName)){
				differenceSchema(element1.getSchema(tName),element2.getSchema(tName));
			}
		}
		return element1;
	}

	private void differenceSchema(DBSchema pSchema1, DBSchema pSchema2) {
		for(String tTable:pSchema1.getTableNames()){
			if(pSchema2.hasTable(tTable)){
				differenceTable(pSchema1.getTable(tTable),pSchema2.getTable(tTable));
			}
		}
	}

	private void differenceTable(DBTable pTable1, DBTable pTable2) {
		DBRow tRow = null;
		for(String tRowName:pTable1.getRowNames()){
			tRow = pTable1.getRow(tRowName);
			if(pTable2.hasTheSameRow(tRow)){
				pTable1.removeRow(tRowName);
			}
		}
	}

	public void setOperand(DBDatabase pElement1, DBDatabase pElement2) {
		element1 = pElement1;
		element2 = pElement2;
	}
	
	public void setArgument(String pArg) {
	}
}
